<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <!--<div class="layui-inline">
                            <label class="layui-form-label">设备类型</label>
                            <div class="layui-input-inline">
                                <select id="ename" name="tid"></select>
                            </div>
                        </div>-->
                        <div class="layui-inline">
                            <label class="layui-form-label">车间</label>
                            <div class="layui-input-inline">
                                <select id="wosId" name="wosId">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <input type="text" name="state" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">品牌</label>
                            <div class="layui-input-inline">
                                <input type="text" name="brand" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        //使用jquery
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        //查询所有车间并在下拉框中显示
        $.get("/workshop/show", function (data) {
            var content = "";
            var workshops = data.data
            for (var i = 0; i < workshops.length; i++) {
                var ws = workshops[i];
                content += '<option value="' + ws.wosId + '">' + ws.wsname + '</option>'
            }
            $("#wosId").append(content);
            /*重新渲染页面*/
            form.render();
        });

        //查询所有设备类型并在下拉框中显示
        /*$.get("/equipmentType/show", function (data) {
            var content = "";
            var equipmentType = data.data
            for (var i = 0; i < equipmentType.length; i++) {
                var eq = equipmentType[i];
                content += '<option value="' + eq.tid + '">' + eq.ename + '</option>'
            }
            $("#ename").html(content);
            /!*重新渲染页面*!/
            form.render();
        });*/
        table.render({
            elem: '#currentTableId',
            url: '/equipment/show',

            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'eid',title: '设备编号', templet: '<div>{{d.equipment.eid}}</div>'},
                {field: 'tid', hide: true, templet: '<div>{{d.equipment.tid}}</div>'},
                {field: 'ename', title: '设备类型名称', templet: '<div>{{d.equipmentType.ename}}</div>'},
                {field: 'description', title: '描述', templet: '<div>{{d.equipmentType.description}}</div>'},
                {field: 'model', title: '型号', templet: '<div>{{d.equipment.model}}</div>'},
                {field: 'brand', title: '品牌', templet: '<div>{{d.equipment.brand}}</div>'},
                {field: 'standards', title: '规格', templet: '<div>{{d.equipment.standards}}</div>'},
                {field: 'wsname', title: '车间', templet: '<div>{{d.workShop.wsname}}</div>'},
                {field: 'price', title: '价格', templet: '<div>{{d.equipment.price}}</div>'},
                {field: 'state', title: '状态', templet: '<div>{{d.equipment.state}}</div>'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20],
            limit: 10,
            page: true,
            skin: 'line'
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //console.log(data);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    // tid: data.field.tid,
                    wosId: data.field.wosId,
                    state: data.field.state,
                    brand: data.field.brand
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        //添加
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加设备类型',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '60%'],
                    content: '../../page/equipment/addEquipment.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
        table.on('tool(currentTableFilter)', function (obj) {
            var equipment = obj.data.equipment,
                equipmentType = obj.data.equipmentType,
                workshop = obj.data.workShop;
            var wosId = workshop.wosId;
            //将该列的车间id存入缓存
            layui.sessionData('workshop', {
                key: 'wosId',
                value: wosId
            });
            if (obj.event === 'edit') {
                //layer.open→打开一个弹出层
                var index = layer.open({
                    //标题
                    title: '编辑设备',
                    //基本层类，可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                    type: 2,
                    shade: 0.2,
                    //最大最小化
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '60%'],
                    //内容
                    content: '../../page/equipment/editEquipment.html',
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        body.find('#eid').val(equipment.eid);
                        body.find('#model').val(equipment.model);
                        body.find('#brand').val(equipment.brand);
                        body.find('#standards').val(equipment.standards);
                        body.find('#price').val(equipment.price);
                        body.find('#state').val(equipment.state);
                    },
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除吗？', function (index) {
                    obj.del();
                    layer.close(index);
                    // alert(data.tid);
                    $.get("/equipment/delet", {eid: equipment.eid})
                });
            }
        });

    });
</script>

</body>
</html>